/**
 * @作者 7七月
 * @微信公号 林间有风
 * @开源项目 $ http://talelin.com
 * @免费专栏 $ http://course.talelin.com
 * @我的课程 $ http://imooc.com/t/4294850
 * @创建时间 2020-03-27 17:26
 */
package com.ruoyi.common.utils;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.Calendar;

@Component
public class OrderUtil {
    // B3230651812529
    private static String[] yearCodes;

    @Value("${wx.year-codes}")
    public void setYearCodes(String yearCodes) {
        String[] chars = yearCodes.split(",");
        OrderUtil.yearCodes = chars;
    }

    public static String makeOrderNo() {
        StringBuffer joiner = new StringBuffer();
        Calendar calendar = Calendar.getInstance();
        String mills = String.valueOf(calendar.getTimeInMillis());
        String micro = LocalDateTime.now().toString();
        String random = String.valueOf(Math.random()*1000).substring(0,2);
        joiner.append(OrderUtil.yearCodes[calendar.get(Calendar.YEAR) - 2024])
                .append(Integer.toHexString(calendar.get(Calendar.MONTH)+1).toUpperCase())
                .append(calendar.get(Calendar.DAY_OF_MONTH))
                .append(mills.substring(mills.length()-5, mills.length()))
                .append(micro.substring(micro.length()-3, micro.length()))
                .append(random);
        return joiner.toString();

    }
    // 生成唯一核销码（12位时间戳+4位随机数）
    public static String generateVerificationCode() {
        String timestamp = String.valueOf(System.currentTimeMillis()).substring(1); // 取后11位
        String randomNum = RandomStringUtils.randomNumeric(4);
        return timestamp + randomNum;
    }


    public static BigDecimal calculatePercentage(BigDecimal base, BigDecimal percentage) {
        return base.multiply(percentage).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
    }

    public static String yuanToFenPlainString(BigDecimal p){
        p = p.multiply(new BigDecimal("100"));
        return toPlain(p);
    }

    public static String toPlain(BigDecimal p){
        return p.stripTrailingZeros().toPlainString();
    }

    public static BigDecimal calculateProduct(long targetNum,  BigDecimal baseCost) {
        return baseCost.multiply(BigDecimal.valueOf(targetNum));
    }

    public static String timestamp10(){
        Long timestamp13 = Calendar.getInstance().getTimeInMillis();
        String timestamp13Str = timestamp13.toString();
        return timestamp13Str.substring(0, timestamp13Str.length() - 3);
    }


    // 字母和数字的字符集
    private static final String CHAR_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    // 随机数生成器
    private static final SecureRandom random = new SecureRandom();

    // 生成核销码的方法
    public static String generateVerificationCode(int length) {
        StringBuilder verificationCode = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHAR_SET.length());  // 获取字符集的随机位置
            verificationCode.append(CHAR_SET.charAt(index)); // 根据随机位置生成字符
        }

        return verificationCode.toString(); // 返回生成的核销码
    }

    public static String formatForAlipay(BigDecimal amount) {
        if (amount == null) {
            throw new IllegalArgumentException("金额不能为空");
        }
        // 四舍五入到两位小数
        BigDecimal formattedAmount = amount.setScale(2, RoundingMode.HALF_UP);
        // 转换为字符串
        return formattedAmount.toString();
    }

   /* public static void main(String[] args) {
        // 生成一个8位的核销码
        String verificationCode = generateVerificationCode(8);
        System.out.println("生成的核销码是：" + verificationCode);
    }*/

}
